import '@static/ueditor/ueditor.config'
import '@static/ueditor/ueditor.all'
import '@static/ueditor/lang/zh-cn/zh-cn'
export default {
  data () {
    return {
      editorId: 'editor' + Math.random().toString().slice(-4),
      content: ''
    }
  },
  methods: {
    initUEditor () {
      this.ue = UE.getEditor(this.editorId, {
        toolbars: [
            ['undo', 'redo', '|', 'fontsize', 'fontfamily'],
            ['blockquote', 'horizontal', '|', 'removeformat', 'formatmatch', 'link', 'unlink', 'bold', 'italic', 'underline'],
            ['forecolor', 'backcolor', '|', 'indent', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', 'insertorderedlist', 'insertunorderedlist']
        ],
        initialFrameWidth: 418,
        initialFrameHeight: 300,
        minFrameHeight: 300,
        allHtmlEnabled: true,
        scaleEnabled: false,
        wordCount: false,
        elementPathEnabled: false,
        autoFloatEnabled: false,
        iframeCssUrl: ''
      })
      this.ue.addListener('contentChange', () => {
        this.content = this.ue.getContent()
      })
      this.ue.addListener('reset', () => {
        this.$nextTick()
          .then(() => {
            this.setContent(this.content)
          })
      })
      this.setContent(this.content)
    },
    // 设置内容
    setContent (cont) {
      if (!this.ue.isReady) {
        this.ue.addListener('ready', () => {
          this.ue.setContent(cont)
        })
      } else {
        this.ue.setContent(cont)
      }
    },
    // 重置
    reset () {
      this.ue.reset()
    }
  }
}
